<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Usersinvoices extends Model
{
    protected $table = 'users_invoices';
    protected $primaryKey = 'id';

    public static function getUsersLastInvoices($users_id)
    {
        return self::where("users_id", $users_id)->orderBy("id", "DESC")->first();
    }

    public static function getUsersInvoices($users_id,$authority,$title)
    {
        $query = self::select("users_invoices.*","users.nike","users.avatar")->leftJoin('users', 'users_invoices.users_id', '=', 'users.id');
        if ($authority != 3) $query->where("users_invoices.users_id",$users_id);
        if (!empty($title)) $query->where("users_invoices.company_name","like","%".$title."%");
        return $query->orderBy("users_invoices.id","DESC")->paginate(25);
    }

    public static function insertInvoices($users_id,$company_name,$company_tax_number,$email,$price)
    {
        return self::insert(["users_id"=>$users_id,"company_name"=>$company_name,"company_tax_number"=>$company_tax_number,"email"=>$email,"price"=>$price,"created_at"=>time()]);
    }

    public static function updateStatus($id,$images)
    {
        return self::where("id",$id)->update(["status"=>2,"images"=>$images]);
    }
}
